<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />

    <!-- 文章信息区域 -->
    <div class="article-container" v-if="article">
      <!-- 文章标题 -->
      <h1 class="art-title">{{article.title}}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="article.aut_name" :label="article.pubdate | dateFormat">
        <template #icon>
          <!-- 头像 -->
          <img :src="article.aut_photo" alt="" class="avatar">
        </template>
        <template #default>
          <div>
            <!-- 是否关注了作者 -->
            <van-button type="info" size="mini" v-if="article.is_followed" @click="setUnfollow">已关注</van-button>
            <van-button icon="plus" type="info" size="mini" plain v-else @click="setFollow">关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="article.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button icon="good-job" type="danger" size="small" v-if="article.attitude===1" @click="delLike">已点赞</van-button>
        <van-button icon="good-job-o" type="danger" plain size="small" v-else @click="addLike">点赞</van-button>
      </div>
    </div>
    <!-- 文章评论组件 -->
    <art-cmt :art-id="id"></art-cmt>
  </div>
</template>
<script>
import { getArticleDetailAPI, followUserApi, unfollowUserApi, addLikeApi, delLikeApi } from '@/api/articleApi.js'
// 导入文章评论
import ArtCmt from '@/components/ArtCmt/ArtCmt.vue'
// 导入 highlight.js
import hljs from 'highlight.js'
export default {
  components: {
    // 注册文章评论
     ArtCmt
  },
    name: 'ArticleDetail',
    // 通过props接收到的id为文章id
    props: ['id'],
    data () {
        return {
            // 文章详情
            article: null
        }
    },
    created () {
        this.initArticleDetail()
    },
    // 组建的DOM更新完成之后，进行高亮
    updated () {
      if (this.article) {
        // 调用此方法，查询dom中code标签，完成美化
      hljs.highlightAll()
      }
    },
    methods: {
        // 初始化数据
       async initArticleDetail () {
       const { data: res } = await getArticleDetailAPI(this.id)
       if (res.message === 'OK') {
        //    转存数据
           this.article = res.data
           console.log(this.article)
       }
        },
        // 点击按钮，关注作者
       async setFollow () {
            // 为了防止作者的id也是大数，最好调用.toString方法
         const { data: res } = await followUserApi(this.article.aut_id.toString())
         if (res.message === 'OK') {
            //  提示用户关注成功
             this.$toast.success('关注成功')
            //  在客户端为article对象的is_followed属性赋值为true
             this.article.is_followed = true
         }
        },
        // 点击按钮，取消关注
       async setUnfollow () {
         const res = await unfollowUserApi(this.article.aut_id.toString())
        //  直接基于res.status来判断是否成功
         if (res.status === 204) {
            //  提示用户
             this.$toast.success('取关成功')
              //  在客户端为article对象的is_followed属性赋值为false
             this.article.is_followed = false
         }
        },
        // 点赞功能
        async addLike () {
          const { data: res } = await addLikeApi(this.id)
          if (res.message === 'OK') {
            this.$toast.success('点赞成功')
           this.article.attitude = 1
          }
        },
        // 取消点赞
        async delLike () {
         const res = await delLikeApi(this.id)
         if (res.status === 204) {
           this.$toast.success('取消点赞成功')
            this.article.attitude = -1
         }
        }

    }
}
</script>
<style lang="less" scoped>
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}

</style>
